package com.croqodile.test{
    import asunit.framework.*;
    import com.croqodile.test.*;
    import com.croqodile.test.support.*;
    import com.senocular.utils.Output;
    import com.croqodile.*;
    
    public class SerializationTest extends TestCase {
	
	override protected function setUp():void {
	    IslandObject.reset();
	}
	
	override protected function tearDown():void {
	}
	
	public function testSerializesToString():void {
	    var controller:DumbController = DumbController.create(DataIsland);
	    var island:DataIsland = DataIsland(controller.island());

	    var str:String = island.snapshot();
	    assertTrue("Length of serialized string should be > 0.", str.length > 0);
	}

	public function testSimpleSerializeAndDeserialize():void {
	    var controller:DumbController = DumbController.create(DataIsland);
	    var island:DataIsland = DataIsland(controller.island());
	    controller.triggerExternalMessage("10000@[\"createAvatar\",1,[\"aemon\"]]");
	    controller.triggerExternalMessage("10001@[\"createAvatar\",1,[\"max\"]]");

	    var controller2:DumbController = DumbController.create(DataIsland);
	    var island2:DataIsland = DataIsland(controller2.island());
	    island2.initFromSnapshot(island.snapshot());
	    assertTrue("There should still be 2 avatars", island2.avatars().length == 2);
	}


	public function testSerializeMessageQ():void {
	    var controller:DumbController = DumbController.create(DataIsland);
	    var island:DataIsland = DataIsland(controller.island());
	    controller.triggerExternalMessage("10000@[\"setOff\",1,[20,\"doSomething\"]]");
	    controller.triggerExternalMessage("10001@[\"setOff\",1,[40,\"doSomething\"]]");
	    assertTrue("There should be one internal message on the q.", island.msgQ().length() == 2);

	    var controller2:DumbController = DumbController.create(DataIsland);
	    var island2:DataIsland = DataIsland(controller2.island());
	    assertTrue("There should be no messages on this fresh q.", island2.msgQ().length() == 0);
	    island2.initFromSnapshot(island.snapshot());
	    assertTrue("There should be one internal message on the new q.", island2.msgQ().length() == 2);
	}

    }
}